module Round_Fuction (
    input kgen_en,     //1: 生成轮密钥 0： 生成加解密数据
    
    input [31:0] x0_i,
    input [31:0] x1_i,
    input [31:0] x2_i,
    input [31:0] x3_i,

    input [31:0] rk_i, //rk_i or ck_i 

    output [31:0] x4_o
);

wire [31:0] A;
wire [31:0] B;
wire [31:0] C;
wire [31:0] D;




//step1: x1 ^ x2 ^ x3 ^rk
assign A = x1_i ^ x2_i ^ x3_i ^ rk_i;

//step2: T function: B=(b0, b1, b2, b3) = (sbox(a0), sbox(a1), sbox(a2), sbox(a3))
SBox_32b SBox_32b_0(
    .data_i(A),
    .data_o(B)
);

//step3:  C = L'(B)  or L(B)
assign C = (kgen_en)?   B ^ ({B[18:0],B[31:19]}) ^ ({B[8:0],B[31:9]}) :                                             //L'(B) = B ^ (B<<<13) ^ B(<<<23)
                        B ^ ({B[29:0],B[31:30]}) ^ ({B[21:0],B[31:22]}) ^ ({B[13:0],B[31:14]}) ^ {B[7:0],B[31:8]};  //L(B)  = B ^ (B<<<2) ^ (B<<<10) ^ (B<<<18) ^(B<<<24)

//step4: rk = D = C ^ x0
assign D = C ^ x0_i; 

//output
assign x4_o = D;

endmodule //Round_Fuction